package pingan.com.imandtwo.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import pingan.com.imandtwo.R;
import pingan.com.imandtwo.activity.ChannelManagementActivity;
import pingan.com.imandtwo.fragments.newsfragments.FiveFragment;
import pingan.com.imandtwo.fragments.newsfragments.FourFragment;
import pingan.com.imandtwo.fragments.newsfragments.OneFragment;
import pingan.com.imandtwo.fragments.newsfragments.ThreenFragment;
import pingan.com.imandtwo.fragments.newsfragments.TwoFragment;

/**
 * Created by xsj on 2018/2/1.
 * 这是首页
 */

public class NewsFragment extends Fragment {

    @BindView(R.id.nts)
    TabLayout tab;
    @BindView(R.id.viewpager)
    ViewPager vp;
    Unbinder unbinder;
    @BindView(R.id.add_white)
    ImageView addWhite;
    private List<String> list = new ArrayList<String>();
//    private List<Fragment> fragmentList = new ArrayList<>();
//    private String[] str = new String[]{"推荐","科技咨询","开放创新","主题活动","科技需求","开放创新","开放创新","开放创新","开放创新","开放创新","开放创新","开放创新"};


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.newsfragment, container, false);

        //黄油刀
        unbinder = ButterKnife.bind(this, view);

        //tab的标题
        list.add("推荐");
        list.add("科技咨询");
        list.add("开放创新");
        list.add("主题活动");
        list.add("科技需求");

        //适配器
        vp.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                Fragment fragment = null;
                //当选中的位置是对应的索引值的话 就加载那个Fragment
                switch (position) {
                    case 0:
                        fragment = new OneFragment();
                        break;
                    case 1:
                        fragment = new TwoFragment();
                        break;
                    case 2:
                        fragment = new ThreenFragment();
                        break;
                    case 3:
                        fragment = new FourFragment();
                        break;
                    case 4:
                        fragment = new FiveFragment();
                        break;

                    default:
                        break;
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return list.size();
            }

            //设置tablelayout标题
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }
        });

//进行关联
        tab.setupWithViewPager(vp);
        addWhite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(),ChannelManagementActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
